<template>
	<view>
		<view class="list">
			<view class="li" v-for="(item,index) in rank" :key="item._id" @click="toDetail(item)">
				<view class="img">
					<image :src="item.fileID" mode="widthFix"></image>
				</view>
				
				<view class="detail">
					<view class="name">名字：{{item.title}}</view>
					<view class="adress">地址：{{item.location}}</view>
					<view class="grade">评分：{{item.rate}}</view>
				</view>
				<view class="reason">推荐理由:{{item.content}}</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		
		data() {
			return {
				search_tar: "",
				rank: [],
			};
		},
		onLoad: function(option) {
			//转换this
			let _this = this 
			const eventChannel = this.getOpenerEventChannel()
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			eventChannel.on('tar', function(data) {
				_this.search_tar = data.data
				console.log(_this.search_tar)
				uniCloud.callFunction({
					name: "search",
					data: {
						name: _this.search_tar
					}
				}).then(res=> {
					console.log(res)
					if(res.result.data.length == 0)
					return uni.showToast({
						title:"暂无此美食",
						duration:2000
					})
					_this.rank = res.result.data
					
				}).catch(err=> {
					console.log(err)
				})
			})
			// const db = uniCloud.database()
			
			
		},
		methods: {
			toDetail(e) {
				//跳转到详情页
				uni.navigateTo({
					url: "/pages/detail_page/index",
					success: function(res) {
						// 通过eventChannel向被打开页面传送数据
						res.eventChannel.emit('acceptDataFromOpenerPage', {
							data: e
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.list {
		.li:nth-child(1) {
			.rank_grade {
				background-color: #FFB400;
			}
		}

		.li:nth-child(2) {
			.rank_grade {
				background-color: #FFB400;
			}
		}

		.li:nth-child(3) {
			.rank_grade {
				background-color: #FFB400;
			}
		}

		.li {
			background-color: #F1D1D1;
			height: 200rpx;
			margin-top: 10rpx;
			position: relative;
			margin-left: 10%;
			margin-right: 10%;
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 2px 2px 20rpx #ccc;
			padding: 10rpx;
			border: 1px solid #ccc;

			.img {
				width: 40%;
				overflow: hidden;
				height: 145rpx;
				
				image {
					margin-top: 10rpx;
					margin-left: 10rpx;
					width: 100%;
					filter: brightness(0.8);
				}
			}

			.rank_grade {
				position: absolute;
				top: 0rpx;
				right: 10rpx;
				background-color: #ccc;
				height: 100rpx;
				width: 80rpx;
			}



			.rank {
				color: #fff;
				font-size: 30rpx;
				position: absolute;
				right: 40rpx;
				top: 10rpx;

				.top {
					position: absolute;
					left: -40rpx;
					top: -10rpx;
				}

				.ranks {
					position: absolute;
					top: 20rpx;
					left: -24rpx;
				}

				.triangle {
					position: absolute;
					top: 70rpx;
					left: -50rpx;
					width: 0;
					height: 0;
					border-left: 40rpx solid transparent;
					border-right: 40rpx solid transparent;
					border-bottom: 24rpx solid white;
				}
			}

			.detail {
				position: absolute;
				left: 40%;
				top: 10rpx;

				.name {
					margin-top: 10rpx;
					margin-left: 20rpx;
				}

				.adress {
					margin-top: 10rpx;
					margin-left: 20rpx;
				}

				.grade {
					margin-top: 10rpx;
					margin-left: 20rpx;
				}
			}

			.reason {
				margin-left: 10rpx;
				font-weight: bold;
			}
		}
	}
</style>
